@file:Suppress("unused")
package com.example.kotlin.extension

import xyz.erupt.annotation.config.*
import xyz.erupt.core.query.*
import xyz.erupt.core.service.*
import xyz.erupt.core.view.*

/**
 * 本文件用kotlin的泛型实例化技巧对IEruptDataService接口的成员方法做了拓展
 * 便于以泛型的写法调用数据源,例如:  val student = dataService.findDataById<Student>(1)
 *
 * 关于IEruptDataService接口,详情参阅 https://www.yuque.com/erupts/erupt/giw1e5
 *
 * 友情提醒：(1)IEruptDataService默认用的JPA实现,如果对JPA不熟悉,请慎用!!! 因为JPA的行为可能跟你想象的不太一样。
 *         (2)更建议使用EruptDao,而不是IEruptDataService
 */
@Comment("根据主键id获取数据(kotlin拓展)")
inline fun <reified MODEL : Any> IEruptDataService.findDataById(@Comment("主键值") id: Any): MODEL? {
    return this.findDataById(EruptModel(MODEL::class.java), id)?.let { it as MODEL }
}

@Comment("查询分页数据(kotlin拓展)")
inline fun <reified MODEL : Any> IEruptDataService.queryList(
    @Comment("分页对象") page: Page,
    @Comment("条件") eruptQuery: EruptQuery,
): Page {
    return this.queryList(EruptModel(MODEL::class.java), page, eruptQuery)
}

@Comment("根据列查询相关数据(kotlin拓展)")
inline fun <reified MODEL : Any> IEruptDataService.queryColumn(
    @Comment("列信息") columns: List<Column>,
    @Comment("条件") eruptQuery: EruptQuery,
): Collection<Map<String, Any>> {
    return this.queryColumn(EruptModel(MODEL::class.java), columns, eruptQuery)
}

@Comment("添加数据(kotlin拓展)")
inline fun <reified MODEL : Any> IEruptDataService.addData(@Comment("数据对象") obj: MODEL) {
    return this.addData(EruptModel(MODEL::class.java), obj)
}

@Comment("修改数据(kotlin拓展)")
inline fun <reified MODEL : Any> IEruptDataService.editData(@Comment("数据对象") obj: MODEL) {
    return this.editData(EruptModel(MODEL::class.java), obj)
}

@Comment("删除数据(kotlin拓展)")
inline fun <reified MODEL : Any> IEruptDataService.deleteData(@Comment("数据对象") obj: MODEL) {
    return this.deleteData(EruptModel(MODEL::class.java), obj)
}